package com.crrc.dataparse.monitor.rsp.service.parse.treat.templatepara.datatrans;

import com.crrc.dataparse.templatepara.IDataTrans;
import com.crrc.dataparse.templatepara.datatrans.utils.DataTranslator;

import java.util.Date;

/*
     8字节时间戳转换
 */

public class Binary8ToUTCTime extends IDataTrans {
    public Binary8ToUTCTime() {
    }
    @Override
    public String trans(String value, double resolusion, double offset) throws Exception {
        if (!DataTranslator.isValid(value)) {
            return null;
        } else {
            int year = Integer.parseInt(value.substring(0, 4), 2) * 10 + Integer.parseInt(value.substring(4, 8), 2) + 100;
            int month = Integer.parseInt(value.substring(8, 16), 2);
            int day = Integer.parseInt(value.substring(16, 24), 2);
            int h = Integer.parseInt(value.substring(24, 32), 2);
            int m = Integer.parseInt(value.substring(32, 40), 2);
            int s = Integer.parseInt(value.substring(40, 48), 2);
            int ms = Integer.parseInt(value.substring(48, 64), 2);
            Date date = new Date(year, month - 1, day, h, m, s);
            long time = date.getTime() + (long)ms;
            return String.valueOf(time);
        }
    }
}

